package com.xingkong.web.controller.product;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xingkong.common.annotation.Log;
import com.xingkong.common.core.controller.BaseController;
import com.xingkong.common.core.domain.AjaxResult;
import com.xingkong.common.core.page.TableDataInfo;
import com.xingkong.common.core.text.Convert;
import com.xingkong.common.enums.BusinessType;
import com.xingkong.common.utils.poi.ExcelUtil;
import com.xingkong.product.domain.Specification;
import com.xingkong.product.service.SpecificationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 规格Controller
 *
 * @author yiyue
 * @date 2020-03-15
 */
@Controller
@RequestMapping("/product/specification")
public class SpecificationController extends BaseController {
    private String prefix = "product/specification";

    @Autowired
    private SpecificationService specificationService;

    @RequiresPermissions("product:specification:view")
    @GetMapping()
    public String specifications() {
        return prefix + "/specification";
    }

    /**
     * 查询规格列表
     */
    @RequiresPermissions("product:specification:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Specification specification) {
        startPage();
        return getDataTable(specificationService.list(new QueryWrapper<Specification>(specification)));
    }



    @GetMapping("/select")
    public String select(ModelMap mmap) {
        List<Specification> specs  = specificationService.list();
        mmap.put("specs",specs);
        return prefix + "/select";
    }

    /**
     * 导出规格列表
     */
    @RequiresPermissions("product:specifications:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Specification specifications) {
        List<Specification> list = specificationService.list(new QueryWrapper<>());
        ExcelUtil<Specification> util = new ExcelUtil<Specification>(Specification.class);
        return util.exportExcel(list, "specifications");
    }

    /**
     * 新增规格
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存规格
     */
    @RequiresPermissions("product:specifications:add")
    @Log(title = "规格", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Specification specification) {
        return toAjax(specificationService.save(specification));
    }

    /**
     * 修改规格
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Specification specification = specificationService.getById(id);
        mmap.put("specification", specification);
        return prefix + "/edit";
    }


    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        Specification specification = specificationService.getById(id);
        mmap.put("spec", specification);
        mmap.put("specs",specificationService.list());
        return "product/item/item";
    }

    /**
     * 修改保存规格
     */
    @RequiresPermissions("product:specifications:edit")
    @Log(title = "规格", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Specification specification) {
        return toAjax(specificationService.updateById(specification));
    }

    /**
     * 删除规格
     */
    @RequiresPermissions("product:specifications:remove")
    @Log(title = "规格", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(specificationService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }





}
